gulp = require('gulp')

runSequence =require('run-sequence')

del = require('del')

uglify = require('gulp-uglify')

concat =require('gulp-concat')

minifyCss = require('gulp-minify-css')

browserSync = require('browser-sync').create()

gulp.task('default',function(callback){
   return runSequence(['clean'],['build'],['serve','watch'],callback)
})

gulp.task('clean',function(callback){
    return del(['./dist/'],callback)
})

gulp.task('build',function(callback){
    return  runSequence(['copy','miniJs','miniCss'],callback)
})

gulp.task('copy',function(){
    return gulp.src('./src/**/*.*')
        .pipe(gulp.dest('./dist/'))
})

gulp.task('miniJs',function(){
    return gulp.src('./src/**/*.js')
        .pipe(gulp.dest('./dist/'))
})

gulp.task('miniCss',function(){
    return gulp.src('./src/**/*.css')
        .pipe(minifyCss())
        .pipe(concat('app.css',{newLine :'\n\n'}))
        .pipe(gulp.dest('/dist/'))
})

gulp.task('concat',function(){
    return gulp.src('./src/*.js')
        .pipe(concat('all.js',{newLine: ':\n'}))
        .pipe(gulp.dest('./dist/'))
})

gulp.task('serve',function(){
    return  browserSync.init({
        server:{
            baseDir:'./dist/'
        },
        port:7411
    })
})

gulp.task('watch',function(){
    return gulp.watch('./src/**/*.*',['reload'])
})

gulp.task('reload',function(callback){
    return runSequence(['build'],['reload-browser'],callback)
})

gulp.task('reload-browser',function(){
    return  browserSync.reload();
})

